package top.zackyoung.bbs.utils;

import top.zackyoung.bbs.entity.PageView;

/**
 * @author: ZackYoung
 * @time: 2020/6/14
 */
public class PageViewUtil {
    public int pageCount(int count, int limitPage) {
        int countPage = 1;
        if (count % limitPage == 0) {
            count = count / limitPage;
            return count;
        } else {
            countPage = count / limitPage + 1;
            return countPage;
        }
    }
    public PageView pageView(Integer  page, int totalRecord, String url, int limitPage){
        PageView pageView = new PageView();

        // 总条数
        pageView.setTotalrecord(totalRecord);
        // 总页数
        int totalPage = pageCount(totalRecord,limitPage);
        pageView.setTotalpage(totalPage);
        pageView.setEndindex(totalPage);
        pageView.setCurrentpage(1);

        if (page!=null){

            pageView.setCurrentpage(page);
            if (page<=0){
                pageView.setCurrentpage(1);
            }
            if (page>totalPage){
                page=totalPage;
            }
        }
        pageView.setPageInt(page);
        pageView.setOnUrl(String.format(url + (page - 1)));
        pageView.setNextUrl(String.format(url + (page + 1)));
        return pageView;
    }
}
